package iot

import (
	"errors"
	"gorm.io/gorm"
	"iot-base/common/dbobj"
)

type (
	Areas dbobj.Area

	AreasMode struct {
		db *gorm.DB
	}
)

func NewAreasMode(db *gorm.DB) *AreasMode {
	return &AreasMode{db: db}
}

func (a *AreasMode) QueryAreasByParentId(parentId string) ([]Areas, error) {
	var list []Areas
	err := a.db.Model(&Areas{}).Where("parent_id = ?", parentId).Find(&list).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return list, nil
	}
	return list, nil
}
